package com.acme.concurrent.pool.connection;

import com.acme.concurrent.pool.DataSourceException;
import com.acme.concurrent.pool.DisposableDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionSource implements DisposableDataSource<Connection> {

    public ConnectionSource(String connectionString) {
        this.connectionString = connectionString;
    }

    private String connectionString;

    @Override
    public void dispose(Connection connection) throws DataSourceException {
        try {
            connection.close();
        } catch (SQLException e) {
            throw new DataSourceException(e);
        }
    }

    @Override
    public Connection newInstance() throws DataSourceException {
        try {
            return DriverManager.getConnection(connectionString);
        } catch (SQLException e) {
            throw new DataSourceException(e);
        }
    }
}
